package cn.icatw.leetcode.editor.cn;
//给定一个二叉树的根节点 root ，返回 它的 中序 遍历 。
//
//
//
// 示例 1：
//
//
//输入：root = [1,null,2,3]
//输出：[1,3,2]
//
//
// 示例 2：
//
//
//输入：root = []
//输出：[]
//
//
// 示例 3：
//
//
//输入：root = [1]
//输出：[1]
//
//
//
//
// 提示：
//
//
// 树中节点数目在范围 [0, 100] 内
// -100 <= Node.val <= 100
//
//
//
//
// 进阶: 递归算法很简单，你可以通过迭代算法完成吗？
//
// Related Topics 栈 树 深度优先搜索 二叉树 👍 2100 👎 0


import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

//Java：二叉树的中序遍历
public class T94_BinaryTreeInorderTraversal {
    public static void main(String[] args) {
        Solution solution = new T94_BinaryTreeInorderTraversal().new Solution();
        // TO TEST
    }
    //leetcode submit region begin(Prohibit modification and deletion)

    /**
     * Definition for a binary tree node.
     * public class TreeNode {
     * int val;
     * TreeNode left;
     * TreeNode right;
     * TreeNode() {}
     * TreeNode(int val) { this.val = val; }
     * TreeNode(int val, TreeNode left, TreeNode right) {
     * this.val = val;
     * this.left = left;
     * this.right = right;
     * }
     * }
     */
    class Solution {
        public List<Integer> inorderTraversal(TreeNode root) {
            ArrayList<Integer> result = new ArrayList<>();
            //inorder(root, result);
            Stack<TreeNode> stack = new Stack<>();
            //iterativeTraversal(root, stack, result);
            //统一迭代遍历法
            if (root == null) {
                return result;
            }
            stack.push(root);
            while (!stack.isEmpty()) {
                TreeNode node = stack.pop();
                if (node != null) {
                    //右子结点
                    if (node.right != null) {
                        stack.push(node.right);

                    }
                    //根节点
                    stack.push(node);
                    stack.push(null);
                    if (node.left != null) {
                        stack.push(node.left);
                    }
                } else {
                    result.add(stack.pop().val);

                }
            }
            return result;
        }

//        迭代遍历法
/*        private void iterativeTraversal(TreeNode root, Stack<TreeNode> stack, ArrayList<Integer> result) {
            TreeNode cur = root;
            while (cur != null || !stack.isEmpty()) {
                while (cur != null) {
                    stack.push(cur);
                    cur = cur.left;
                }
                cur = stack.pop();
                result.add(cur.val);
                cur = cur.right;
            }
        }*/
        // 递归遍历
        //public void inorder(TreeNode root, List<Integer> res) {
        //    if (root == null) {
        //        return;
        //    }
        //    inorder(root.left, res);
        //    res.add(root.val);
        //    inorder(root.right, res);
        //}
    }
//leetcode submit region end(Prohibit modification and deletion)
      public class TreeNode {
      int val;
      TreeNode left;
      TreeNode right;
      TreeNode() {}
      TreeNode(int val) { this.val = val; }
      TreeNode(int val, TreeNode left, TreeNode right) {
      this.val = val;
      this.left = left;
      this.right = right;
      }
      }
}
